package basic.sort;

/**
 * @创建人 MBY381
 * @创建时间 2022/3/29 18:55
 * @描述 暂无
 */

import java.util.Arrays;


class A_2_QuickSort_RIGHT {


    public static void main(String[] args) {
        int[] nums1 = new int[]{0xE3, 381, 8, 6, 4, 7, 9, 3, 5, 2, 1, 782, 696, 259, 0x15};
        System.out.println("正确答案：    [1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 227, 259, 381, 696, 782]");
        sort(nums1, 0, nums1.length - 1);
        System.out.println(Arrays.toString(nums1));
        int[] nums2 = new int[]{3000,0xE3, 381, 2001,8, 6, 4, 7, 9, 3, 5, 2, 1, 782, 696, 259, 0x15};
        sortOwn(nums2,0,nums2.length-1);
        System.out.println(Arrays.toString(nums2));

        /**
         * 自动标准扫描输入输出
         */
//        Scanner sc = new Scanner(System.in);
//        int n = sc.nextInt();
//        int[] nums = new int[2 * n];
//        for (int i = 0; i < n; i++) {
//            nums[i] = sc.nextInt();
//        }
//        try {
//            sort(nums, 0, nums.length - 1);
////            quickSort(nums,0, nums.length-1);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        for (int i = 0; i < nums.length; i++) {
//            System.out.print(nums[i]);
//            System.out.print(" ");
//        }

    }



    /**
     * @param nums
     * @param startIndex
     * @param endIndex
     */
    static void sort(int[] nums, int startIndex, int endIndex) {

        if (startIndex >= endIndex) {
            return;
        }
        int mid = nums[(startIndex + endIndex) >> 1];
        int left = startIndex, right = endIndex;
        while (left <= right) {
            while (nums[left] < mid)
                left++;
            while (nums[right] > mid)
                right--;

            if (left <= right) {
                int t = nums[right];
                nums[right] = nums[left];
                nums[left] = t;
                left++;
                right--;
            }
        }
        if (startIndex < right)
            sort(nums, startIndex, right);
        if (left < endIndex)
            sort(nums, left, endIndex);
    }

    static void sortOwn(int[] nums, int startIndex, int endIndex) {

        if (startIndex >= endIndex) {
            return;
        }
        int index = nums[startIndex];
        int left = startIndex, right = endIndex;
        while (left <= right) {
            while (nums[left] < index)
                left++;
            while (nums[right] > index)
                right--;

            if (left <= right) {
                int t = nums[right];
                nums[right] = nums[left];
                nums[left] = t;
                left++;
                right--;
            }
        }
        if (startIndex < right)
            sort(nums, startIndex, right);
        if (left < endIndex)
            sort(nums, left, endIndex);
    }
    public static void swap(int[] nums,int left,int right){
        int temp =nums[right];
        nums[right]=nums[left];
        nums[left]=temp;
    }

}